#include "Menu.h"
#include <iostream>

//additional includes
#include <fstream>												//FPW, A1

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{
	cout << "Here is the MAIN MENU \n";
   /* The following code has been written by Eric Heisler, solving problem A3 */
	if(categories.size()>0 && menu_items.size()>0) {
		string menu[categories.size()][menu_items.size()+1];
		for(int i = 0; i < categories.size(); i++) {
			menu[i][0] = categories[i].display();
			int col = 1;
			for(int j = 0; j < menu_items.size(); j++) {
				if(menu_items[j].get_cat_id() == categories[i].get_cat_id()) {
					menu[i][col++] = menu_items[j].display();
				}
			}
		}
		int len = 19;
		for(int i = 0; i < menu_items.size(); i++) {
			for(int j = 0; j < categories.size(); j++) {
				cout << menu[j][i];
				for(int k = 0; k < len - menu[j][i].size(); k++) cout << " ";
				cout << "|";
			}
			cout << "\n";
			if(i == 0) {
				for(int r = 0; r < 4*len+4; r++) {
					cout << "-";
				}
				cout << "\n";
			}
		}
	}
	else cout << "Menu hasn't been read yet. Read in the menu first.\n\n";
	/* End of code written by EMH */
}
void Menu::display_info() const
{
	/* The following code has been written by Eric Heisler, solving problem A3 */
	cout << "Which item would you like to see more info about?\n>>";
	string item;
	cin >> item;
	int index = -1;
	for(int i = 0; i < menu_items.size(); i++) {
		if(menu_items[i].get_menu_item_name() == item) {
			index = i;
			break;
		}
	}
	if(index==-1) {
		cout << "Apologies, there does not appear to be an item bearing that name.\n\n";
		return;
	}
	cout << "Here is the Info requested \n";
	cout << menu_items[index].get_descr() << "\n\n";
	/* End of code written by EMH */
}

void Menu::read()
{  			
	ifstream fileinput;								//FPW, A1

	int ingredient_id;								//FPW, A1
	int recipe_id;									//FPW, A1
	float amt;									//FPW, A1
	string units;									//FPW, A1
	string name;									//FPW, A1
	string chef_name;								//FPW, A1
	string word;									//FPW, A1
	string sentence;								//FPW, A1
	int ning; //number of ingredients						//FPW, A1
	int nrec; //number of recipe & instruction					//FPW, A1
	int i=1; //iterator								//FPW, A1
	//char filename[100];								//FPW, A1
	//string category;
	//cout << "Enter the name of a file and its category:\nName>>";			//FPW, A1
	//cin >> filename;								//FPW, A1
	//cout << "\nCategory>>";
	//cin >> category;

	/* Start: MGM, A2 */
	string catmenuFilename = "..\\sample data\\catmenu.dat";
	string ordersFilename = "..\\sample data\\orders.dat";
	string recipesFilename = "..\\sample data\\recipes.dat";
	/* End: MGM, A2 */

											
	//Read and Store Recipe Data
	//fileinput.open(filename); // opens the file							//FPW, A1
	fileinput.open(recipesFilename.c_str());								//MGM, A2
	if(!fileinput.is_open()) { // file couldn't be opened					//FPW, A1
		cerr << "Error: file could not be opened" << endl;					//FPW, A1
	} 																		//FPW, A1
	else {																	//FPW, A1
		//if(category=="ingredients") {
		fileinput >> ning;													//FPW, A1
		//read and store the ingredients									//FPW, A1
		while (i <= ning) {													//FPW, A1
			fileinput >> ingredient_id;										//FPW, A1       
			fileinput >> recipe_id;											//FPW, A1
			fileinput >> amt;												//FPW, A1
			fileinput >> units;												//FPW, A1       
			fileinput >> name;												//FPW, A1
			Ingredient ing (ingredient_id, recipe_id, amt, units, name); 	//FPW, A1
			Menu::ingredients.push_back(ing);								//FPW, A1
			//ingredients[i-1].displaym();									//FPW, A1
			i++;															//FPW, A1
		}																	//FPW, A1
		fileinput >> nrec;													//FPW, A1
		//cout << nrec << endl;												//FPW, A1
		i = 1;																//FPW, A1
		while (i <= nrec) {													//FPW, A1
			fileinput >> recipe_id;											//FPW, A1
			fileinput >> chef_name;											//FPW, A1
			fileinput >> word;												//FPW, A1
			sentence = "";													//FPW, A1
			while (word != "#") {											//FPW, A1
				sentence = sentence + " " + word;							//FPW, A1
				fileinput >> word;											//FPW, A1
			}																//FPW, A1
			Instructions ins (sentence);									//FPW, A1
			Recipe rec (recipe_id, chef_name, ins);							//FPW, A1
			Menu::recipes.push_back(rec);									//FPW, A1
			//recipes[i-1].displaym();										//FPW, A1
			//cout << endl;													//FPW, A1
			i++;															//FPW, A1
		}																	//FPW, A1
	}
	fileinput.close();														

	//Read and Store Category and Menu Data
	fileinput.open(catmenuFilename.c_str());
	if(!fileinput.is_open()) { // file couldn't be opened                                     
		cerr << "Error: file could not be opened" << endl;			
	} 										
	else {	
		/* The following code is written by Eric Heisler, and solves the problem A3 */
		//else if(category=="menu") {
		if(Menu::menu_items.size()==0 && Menu::categories.size()==0) {
			int type;
			fileinput >> type;	
			for(int i = 0; i < type; i++) {
				int cat_id;
				string cat_name;
				fileinput >> cat_id >> cat_name;
				Category cat (cat_id, cat_name);
				Menu::categories.push_back(cat);
			}
				
			int num_menu_items;
			fileinput >> num_menu_items;
			for(int i = 0; i < num_menu_items; i++) {
				int menu_item_id;
				int cat_id;
				int recipe_id;
				string menu_item_name;
				double price;
				string descr_string;			
		
				fileinput >> menu_item_id >> cat_id >> recipe_id >> menu_item_name >> price;
					
				string next = "";
				while(next!="#") {
					fileinput >> next;
					descr_string += " " + next;
				}
				Description descr (descr_string);
					
				Menu_Item menu_item (menu_item_id, cat_id, recipe_id, menu_item_name, price, descr);
				Menu::menu_items.push_back(menu_item);
			}
		}
	}
		/* End of code written by EMH */
	fileinput.close();								//FPW, A1

	/* Start: MGM, A2 */
	//Read and Store Order and Order_Item Data
	fileinput.open(ordersFilename.c_str());
	if(!fileinput.is_open()) 
	{ 
		// file couldn't be opened
		cerr << "Error: file could not be opened" << endl;			
	} 										
	else
	{
		int numOrder, orderID, serverID ,tableID, y, m, d, h, min, numOrderItem, ordID, MenuID, prodQty;
		char seatID;
		
		//read file (Order)
		fileinput >> numOrder;
		
		for (int i = 0; i < numOrder; i++)
		{
			fileinput >> orderID >> serverID >> tableID >> y >> m >> d >> h >> min;
			
			Order temp(orderID, serverID, tableID, Date(y, m, d), Time(h, min));
			orders.push_back(temp);
		}
		
		//read file (Order Item)
		fileinput >> numOrderItem;
		
		for (int i = 0; i < numOrderItem; i++)
		{
			fileinput >> seatID >> ordID >> MenuID >> prodQty;
			
			Order_Item temp1(seatID, ordID, MenuID, prodQty);
			order_items.push_back(temp1);
		}
	}
	fileinput.close();

	cout << "\nRecipe, Ingredient, Order, Order Item, Category, and Menu Data have been stored succesfully \n\n";
	
	/* End: MGM, A2 */
	
	//cout << "File " << filename << "has been read and stored succesfully" << endl;	//FPW, A1
	//}
} 
void Menu::show() const 
{
	cout << "Show queries \n";
		
}
void Menu::find() const 
{
	cout << "Find queries \n";
}
void Menu::update()  
{
	cout << "Update queries \n";
}
bool Menu::exit() 											//FPW, A1
{															//FPW, A1
	bool confirm_bool=false; 								//FPW, A1
	char confirm; 											//FPW, A1
	cout << "Are you sure you want to exit? (y/n) \n> "; 	//FPW, A1
	cin >> confirm; 										//FPW, A1
	if (confirm == 'y' || confirm == 'Y') { 				//FPW, A1
		confirm_bool = true; 								//FPW, A1
	} 														//FPW, A1
	else if (confirm == 'n' || confirm == 'N') { 			//FPW, A1
		confirm_bool = false; 								//FPW, A1
	} 														//FPW, A1
	else {													//FPW, A1
		cout << "Invalid input. Exit aborted" << endl;		//FPW, A1
	}														//FPW, A1
	return confirm_bool; 									//FPW, A1
}															//FPW, A1